home *** CD-ROM | disk | FTP | other *** search
/ NASA Climatology Interdisciplinary Data Collection / NASA Climatology Interdisciplinary Data Collection - Disc 4.iso / software / grads / lib / cbarn.gs < prev    next >
Encoding:
Text File  |  1998-04-23  |  4.8 KB  |  242 lines

  1. *
  2. *  Script to plot a colorbar
  3. *
  4. *  The script will assume a colorbar is wanted even if there is 
  5. *  not room -- it will plot on the side or the bottom if there is
  6. *  room in either place, otherwise it will plot along the bottom and
  7. *  overlay labels there if any.  This can be dealt with via 
  8. *  the 'set parea' command.  In version 2 the default parea will
  9. *  be changed, but we want to guarantee upward compatibility in
  10. *  sub-releases.
  11. *
  12. *
  13. *    modifications by mike fiorino 940614
  14. *
  15. *    - the extreme colors are plotted as triangles
  16. *    - the colors are boxed in white
  17. *    - input arguments in during a run execution:
  18. *    run cbarn sf vert xmid ymid
  19. *
  20. *    sf   - scale the whole bar 1.0 = original 0.5 half the size, etc.
  21. *    vert - 0 FORCES a horizontal bar = 1 a vertical bar
  22. *    xmid - the x position on the virtual page the center the bar
  23. *    ymid - the x position on the virtual page the center the bar
  24. *
  25. *    if vert,xmid,ymid are not specified, they are selected
  26. *    as in the original algorithm
  27. *  
  28.  
  29. function colorbar (args)
  30.  
  31. sf=subwrd(args,1)
  32. vert=subwrd(args,2)
  33. xmid=subwrd(args,3)
  34. ymid=subwrd(args,4)
  35.  
  36. if(sf='');sf=1.0;endif
  37.  
  38. *
  39. *  Check shading information
  40. *
  41.   'query shades'
  42.   shdinfo = result
  43.   if (subwrd(shdinfo,1)='None') 
  44.     say 'Cannot plot color bar: No shading information'
  45.     return
  46.   endif
  47.  
  48. *  Get plot size info
  49. *
  50.   'query gxinfo'
  51.   rec2 = sublin(result,2)
  52.   rec3 = sublin(result,3)
  53.   rec4 = sublin(result,4)
  54.   xsiz = subwrd(rec2,4)
  55.   ysiz = subwrd(rec2,6)
  56.   ylo = subwrd(rec4,4)
  57.   xhi = subwrd(rec3,6)
  58.   xd = xsiz - xhi
  59.  
  60.   ylolim=0.6*sf
  61.   xdlim1=1.0*sf
  62.   xdlim2=1.5*sf  
  63.   barsf=0.8*sf
  64.   yoffset=0.2*sf
  65.   stroff=0.05*sf
  66.   strxsiz=0.12*sf
  67.   strysiz=0.13*sf
  68. *
  69. *  Decide if horizontal or vertical color bar
  70. *  and set up constants.
  71. *
  72.   if (ylo<ylolim & xd<xdlim1) 
  73.     say "Not enough room in plot for a colorbar"
  74.     return
  75.   endif
  76.   cnum = subwrd(shdinfo,5)
  77. *
  78. *    logic for setting the bar orientation with user overides
  79. *
  80.   if (ylo<ylolim | xd>xdlim1)
  81.     vchk = 1
  82.     if(vert = 0) ; vchk = 0 ; endif
  83.   else
  84.     vchk = 0
  85.     if(vert = 1) ; vchk = 1 ; endif
  86.   endif
  87. *
  88. *    vertical bar
  89. *
  90.  
  91.   if (vchk = 1 )
  92.  
  93.     if(xmid = '') ; xmid = xhi+xd/2 ; endif
  94.     xwid = 0.2*sf
  95.     ywid = 0.5*sf
  96.     
  97.     xl = xmid-xwid/2
  98.     xr = xl + xwid
  99.     if (ywid*cnum > ysiz*barsf) 
  100.       ywid = ysiz*barsf/cnum
  101.     endif
  102.     if(ymid = '') ; ymid = ysiz/2 ; endif
  103.     yb = ymid - ywid*cnum/2
  104.     'set string 1 l 5'
  105.     vert = 1
  106.  
  107.   else
  108.  
  109. *
  110. *    horizontal bar
  111. *
  112.  
  113.     ywid = 0.4
  114.     xwid = 0.8
  115.  
  116.     if(ymid = '') ; ymid = ylo/2-ywid/2 ; endif
  117.     yt = ymid + yoffset
  118.     yb = ymid
  119.     if(xmid = '') ; xmid = xsiz/2 ; endif
  120.     if (xwid*cnum > xsiz*barsf)
  121.       xwid = xsiz*barsf/cnum
  122.     endif
  123.     xl = xmid - xwid*cnum/2
  124.     'set string 1 tc 5'
  125.     vert = 0
  126.   endif
  127.  
  128.  
  129. *
  130. *  Plot colorbar
  131. *
  132.  
  133.  
  134.   'set strsiz 'strxsiz' 'strysiz
  135.   num = 0
  136.   while (num<cnum) 
  137.     rec = sublin(shdinfo,num+2)
  138.     col = subwrd(rec,1)
  139.     hi = subwrd(rec,3)
  140.     if (vert) 
  141.       yt = yb + ywid
  142.     else 
  143.       xr = xl + xwid
  144.     endif
  145.  
  146.     if(num!=0 & num!= cnum-1)
  147.     'set line 1 1 10'
  148.     'draw rec 'xl' 'yb' 'xr' 'yt
  149.     'set line 'col
  150.     'draw recf 'xl' 'yb' 'xr' 'yt
  151.     if (num<cnum-1)
  152.       if (vert) 
  153.         xp=xr+stroff
  154.         'draw string 'xp' 'yt' 'hi
  155.       else
  156.         yp=yb-stroff
  157.         'draw string 'xr' 'yp' 'hi
  158.       endif
  159.     endif
  160.     endif
  161.  
  162.     if(num = 0 )
  163.  
  164.       if(vert = 1)
  165.  
  166.         xm=(xl+xr)*0.5
  167.         'set line 1 1 10'
  168.         'draw line 'xl' 'yt' 'xm' 'yb
  169.         'draw line 'xm' 'yb' 'xr' 'yt
  170.         'draw line 'xr' 'yt' 'xl' 'yt
  171.  
  172.         'set line 'col
  173.         'draw polyf 'xl' 'yt' 'xm' 'yb' 'xr' 'yt' 'xl' 'yt
  174.  
  175.       else
  176.  
  177.         ym=(yb+yt)*0.5
  178.         'set line 1 1 10'
  179.         'draw line 'xl' 'ym' 'xr' 'yb
  180.         'draw line 'xr' 'yb' 'xr' 'yt
  181.         'draw line 'xr' 'yt' 'xl' 'ym
  182.  
  183.         'set line 'col
  184.        'draw polyf 'xl' 'ym' 'xr' 'yb' 'xr' 'yt' 'xl' 'ym
  185.  
  186.       endif
  187.  
  188.     endif
  189.  
  190.     if (num<cnum-1)
  191.       if (vert)
  192.          xp=xr+stroff 
  193.         'draw string 'xp' 'yt' 'hi
  194.       else
  195.          yp=yb-stroff
  196.         'draw string 'xr' 'yp' 'hi
  197.       endif
  198.     endif
  199.  
  200.     if(num = cnum-1 )
  201.  
  202.       if( vert = 1)
  203.         'set line 1 1 10'
  204.         'draw line 'xl' 'yb' 'xm' 'yt
  205.         'draw line 'xm' 'yt' 'xr' 'yb
  206.         'draw line 'xr' 'yb' 'xl' 'yb
  207.  
  208.         'set line 'col
  209.         'draw polyf 'xl' 'yb' 'xm' 'yt' 'xr' 'yb' 'xl' 'yb
  210.       else
  211.  
  212.         'set line 1 1 10'
  213.         'draw line 'xr' 'ym' 'xl' 'yb
  214.         'draw line 'xl' 'yb' 'xl' 'yt
  215.         'draw line 'xl' 'yt' 'xr' 'ym
  216.  
  217.         'set line 'col
  218.         'draw polyf 'xr' 'ym' 'xl' 'yb' 'xl' 'yt' 'xr' 'ym
  219.         
  220.  
  221.       endif
  222.  
  223.     endif
  224.  
  225.     if (num<cnum-1)
  226.       if (vert) 
  227.         xp=xr+stroff
  228.         'draw string 'xp' 'yt' 'hi
  229.       else
  230.         yp=yb-stroff
  231.        'draw string 'xr' 'yp' 'hi
  232.       endif
  233.     endif
  234.  
  235.     num = num + 1
  236.     if (vert); yb = yt;
  237.     else; xl = xr; endif;
  238.   endwhile
  239. return
  240.